package com.zyw.auth.common;

import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

public final class PasswordHelper {
	private static final String algorithmName = "MD5";
	
	private PasswordHelper() {
	}
	
	/**
	 * 获取密码盐值
	 */
	public static String generateCredentialsSalt() {
		SecureRandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
		return randomNumberGenerator.nextBytes().toHex();
	}
	
	/**
	 * MD5散列加密算法
	 * @param plaintextPassword 密码明文
	 * @param credentialsSalt 密码盐值
	 */
	public static String encryptPasswordMD5(String plaintextPassword, String credentialsSalt) {
		return new SimpleHash(algorithmName, plaintextPassword, ByteSource.Util.bytes(credentialsSalt), 1).toString();
	}
	
}
